from datetime import datetime

from mongoengine import DynamicDocument
from mongoengine.base.fields import ObjectIdField
from mongoengine.fields import DateTimeField, StringField


class OperationTrajectory(DynamicDocument):
    meta = {'collection': 'ctms_operation_trajectory', 'verbose_name': '操作轨迹'}

    ip = StringField(verbose_name='ip地址')
    page = StringField(verbose_name='操作页面')
    detail = StringField(verbose_name='操作详情')
    opetation_user = StringField(verbose_name='操作者')
    project = ObjectIdField(verbose_name='项目')
    operation_time = DateTimeField(verbose_name='操作时间', default=datetime.now())

    def to_json(self):
        data = {
            'id': str(self.id),
            'ip': self.ip,
            'page': self.page,
            'detail': self.detail,
            'opetation_user': self.opetation_user,
            'project': str(self.project) if self.project else None,
            'operation_time': datetime.isoformat(self.operation_time)
        }
        return data
